from app import create_app, db
from app.models import User
from datetime import datetime, timedelta
import random

def create_test_users():
    app = create_app()
    with app.app_context():
        # 测试用户数据
        test_users = [
            {
                'username': 'test_user1',
                'email': 'test1@example.com',
                'password': 'test123',
                'is_active': True,
                'height': 175.5,
                'gender': '男',
                'birth_date': datetime(1990, 1, 1).date(),
                'target_weight': 70.0
            },
            {
                'username': 'test_user2',
                'email': 'test2@example.com',
                'password': 'test123',
                'is_active': True,
                'height': 165.0,
                'gender': '女',
                'birth_date': datetime(1995, 6, 15).date(),
                'target_weight': 55.0
            },
            {
                'username': 'manager1',
                'email': 'manager1@example.com',
                'password': 'manager123',
                'is_active': True,
                'is_admin': True,
                'height': 180.0,
                'gender': '男',
                'birth_date': datetime(1985, 3, 10).date(),
                'target_weight': 75.0
            },
            {
                'username': 'inactive_user',
                'email': 'inactive@example.com',
                'password': 'test123',
                'is_active': False,
                'height': 170.0,
                'gender': '女',
                'birth_date': datetime(1992, 12, 25).date(),
                'target_weight': 60.0
            }
        ]

        # 批量创建更多普通用户
        for i in range(5, 21):  # 创建编号为5-20的用户
            test_users.append({
                'username': f'user{i}',
                'email': f'user{i}@example.com',
                'password': 'test123',
                'is_active': random.choice([True, True, True, False]),  # 75%概率激活
                'height': round(random.uniform(160.0, 185.0), 1),
                'gender': random.choice(['男', '女']),
                'birth_date': (datetime.now() - timedelta(days=random.randint(7300, 14600))).date(),  # 20-40岁
                'target_weight': round(random.uniform(50.0, 80.0), 1)
            })

        # 创建用户
        for user_data in test_users:
            # 检查用户是否已存在
            if not User.query.filter_by(username=user_data['username']).first():
                user = User(
                    username=user_data['username'],
                    email=user_data['email'],
                    is_active=user_data['is_active'],
                    is_admin=user_data.get('is_admin', False),
                    height=user_data['height'],
                    gender=user_data['gender'],
                    birth_date=user_data['birth_date'],
                    target_weight=user_data['target_weight'],
                    created_at=datetime.now() - timedelta(days=random.randint(0, 180))  # 随机创建时间
                )
                user.set_password(user_data['password'])
                db.session.add(user)
                print(f"创建用户: {user_data['username']}")
            else:
                print(f"用户已存在: {user_data['username']}")

        db.session.commit()
        print("\n测试用户创建完成！")
        print("\n测试账号信息：")
        print("普通用户:")
        print("用户名: test_user1  密码: test123")
        print("用户名: test_user2  密码: test123")
        print("管理员:")
        print("用户名: manager1    密码: manager123")
        print("禁用用户:")
        print("用户名: inactive_user  密码: test123")
        print("\n其他测试用户:")
        print("用户名: user5 到 user20  密码: test123")

if __name__ == '__main__':
    # 请求确认
    confirm = input("此操作将添加测试用户到数据库。是否继续？(y/n): ")
    if confirm.lower() == 'y':
        create_test_users()
    else:
        print("操作已取消") 